#!/bin/bash

DIR="$(dirname "$0")"
VARGUID="FFF12B8D-7696-4C8B-A985-2747075B4F50"
NVARGUID="CEF5B9A3-476D-497F-9FDC-E98143E0422C"
if (( $# <= 1 )) ; then
  echo "version: V1.5.26"
  echo "Copyright (c) 2005-2023, Kunlun BIOS, Kunlun Technology (Beijing) Co., Ltd."
  echo "USAGE: sudo ./SetupTool export(import default)"

  echo " "
  version=$($DIR/flashrom -h | grep "flashrom V" | awk -F"\r" '{print $1}')
  echo $version
  exit 1;
fi

space=" "
cmd=$*
plat=""
chip=0
setupcmd=""
countparam=0

flashrom_cmd=""
FlashName=""

plat1="D2000/8"
plat2="FT-2500"
plat3="Hygon"
plat4="Loongson-3A"
plat5="SW"
plat6="FT-2000/4"
plat7="FT2000PLUS"
plat8="3C5000L"
plat9="3A5000"
plat10="3C5000"
plat11="ZHAOXIN"
plat12="S5000C"
plat_all=$plat1$space$plat2$space$plat3$space$plat4$space$plat5$space$plat6$space$plat7$space$plat8$space$plat9$space$plat10$space$plat11$space$plat12

function switchPlat() {
  sPlat=""
  if [[ $1 =~ $plat1 ]] ; then
    sPlat="FT2000"
  elif [[ $1 =~ $plat2 ]] ; then
    sPlat="FT2500"
  elif [[ $1 =~ $plat3 ]] ; then
    sPlat="internal"
  elif [[ $1 =~ $plat11 ]] ; then
    sPlat="internal"
  elif [[ $1 =~ $plat4 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat5 ]] ; then
    sPlat="SW64"
  elif [[ $1 =~ $plat6 ]] ; then
    sPlat="FT2000"
  elif [[ $1 =~ $plat7 ]] ; then
    sPlat="FT2000PLUS"
  elif [[ $1 =~ $plat8 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat9 ]] ; then
    sPlat="LS3A5000"
  elif [[ $1 =~ $plat10 ]] ; then
    sPlat="LS3C5000"
  elif [[ $1 =~ $plat12 ]] ; then
    sPlat="FT5000"
  fi
  echo $sPlat
}

function getCmdPlat() {
  sPlat=""
  for param in $*
  do
    if [[ $param =~ "--plat=" ]] ; then
      sPlat=$(switchPlat $param)
    else
      sPlat=$sPlat
    fi
  done
  echo $sPlat
}

function catCpuInfo() {
	echo `cat /proc/cpuinfo | grep "model name" | awk -F":" '{print $2}' | awk -F" " '{print $1}' | awk 'NR==1'`
}

function getCpuInfoPlat() {
  sPlat=""
  sPlat=$(catCpuInfo)
  sPlat=$(switchPlat $sPlat)
  echo $sPlat
}

function getPlat() {
  sPlat=""
  if [[ $* =~ "--plat=" ]] ; then
    sPlat=$(getCmdPlat $*)
  else 
    sPlat=$(getCpuInfoPlat)
  fi
  echo $sPlat
}

#echo "cmd: "$cmd
plat=$(getPlat $cmd)
#echo "plat ==== "$plat
if [ ! $plat ];then
  echo "plat is NULL,Please try adding --plat=" $plat_all "etc."
#  echo "Unknown or unsupport platform!"
  exit 1;
fi

chmod 777 $DIR/*

for param in $*
do
  if ((countparam > 0));then
    if test $param = "-c" ;then
      chip=1
      flashrom_cmd=$param
    elif test $chip = 1 ;then
      flashrom_cmd=$flashrom_cmd$space$param
      chip=0
    elif test $param = "export" -o $param = "import" -o $param = "default" -o $param = "BootEx" -o $param = "InitCfg" ;then
      setupcmd=$param
    fi
  fi
  countparam=$countparam+1
done

if test -z "$setupcmd" ;then
  echo "version: V1.5.26"
  echo "Copyright (c) 2005-2023, Kunlun BIOS, Kunlun Technology (Beijing) Co., Ltd."
  echo "USAGE: sudo ./SetupTool export(import default)"

  echo " "
  version=$($DIR/flashrom -h | grep "flashrom V" | awk -F"\r" '{print $1}')
  echo $version
  exit 1
fi

if [[ $setupcmd =~ "import" ]] ; then
  if [ ! -f "./config/config" ]; then
    echo -e "\033[41;5mUnable to find the configuration file, Please use the export command first.\033[0m"
    exit 1
  fi
fi
flashmain=0 
flashbak=0
ipmitool raw 0x3E 0xE9 0x01 0x00
if [ $? -ne 0 ]; then
   echo Switching main flash failed
else 
  $DIR/flashrom $flashrom_cmd -p $plat -r $1
  if [ $? -ne 0 ]; then
     echo ""
  else
      flashmain=1
  fi
fi

sleep 1


if [ "$flashmain" -eq 0 ];then 
  ipmitool raw 0x3E 0xE9 0x01 0x01
  if [ $? -ne 0 ]; then
    echo Switching backup flash failed
    exit  1
  else
    $DIR/flashrom $flashrom_cmd -p $plat -r $1
  
    if [ $? -ne 0 ]; then
      echo ""
      ipmitool raw 0x3E 0xE9 0x01 0x00
      sleep 1
      exit 1
    else
        flashbak=1
    fi
  fi
fi




rm -rf $1-unpack*
$DIR/UEFIExtract $1 unpack > /dev/null

rm -rf $DIR/ext
mkdir $DIR/ext
cp -r $1-unpack.dump/*PE32*.bin $DIR/ext

if [ -f "$1-unpack.dump/File_Raw_${NVARGUID}_NVAR_store_info.txt" ]; then
  cp -r $1-unpack.dump/*$NVARGUID*.bin $DIR/ext
  rm -rf ./var
  mkdir ./var
  cp -r $1-unpack.dump/*$NVARGUID*.txt ./var
else
  cp -r $1-unpack.dump/*${VARGUID}*.bin $DIR/ext
  rm -rf ./var
  mkdir ./var
  cp -r $1-unpack.dump/*${VARGUID}*.txt ./var
fi
rm -rf $1-unpack*

FlashName=$(bash $DIR/setuptool_layout.sh)
if [ ! -f "$DIR/Layout" ];then
  echo -e "\033[41;5mERR: File Layout not found!!!\033[0m"
  exit 1;
fi

if [ ! -f $1 ];then
  echo "ERR: File $1 not found!!!"
  exit 1;
fi

$DIR/ifrextract $DIR/ext ./config $1 $setupcmd
if [ $? -ne 0 ]; then
    echo -e "\033[41;5m$setupcmd fail\033[0m"
    exit 1
fi

if test $setupcmd !=  "export" -a $setupcmd != "BootEx" -a $setupcmd != "InitCfg" ; then
  #echo $DIR/flashrom $flashrom_cmd -p $plat -l $DIR/Layout -N -i NVFV -w out.bin
  echo Start updating  flash
  $DIR/flashrom $flashrom_cmd -p $plat -l $DIR/Layout -N $FlashName -w out.bin
  if [ $? -ne 0 ]; then
      echo update fail
      #exit 1
  fi

  if [ "$flashmain" -eq 1 ];then 
    ipmitool raw 0x3E 0xE9 0x01 0x01
    if [ $? -ne 0 ]; then
      echo Switching backup flash failed
    
      #ipmitool raw 0x3E 0xE9 0x01 0x00
    else
      sleep 1
      echo Start updating backup flash
      $DIR/flashrom $flashrom_cmd -p $plat -l $DIR/Layout -N $FlashName -w out.bin

      if [ $? -ne 0 ]; then
        echo ""
      fi

    fi
    ipmitool raw 0x3E 0xE9 0x01 0x00
    sleep 1
  fi

  rm $DIR/Layout
fi

rm -rf $DIR/ext
rm -rf ./var
#rm -rf *.bin
exit $?